package regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author DCSGO
 * @version 1.0
 * 正则表达式应用案例
 * URL
 */
public class Regexp11 {
    public static boolean isURL(String input) {
        //https://www.bilibili.com/video/BV1fh411y7R8/?p=894&spm_id_from=pageDriver&vd_source=b03ab706681712e193fdc7caf9b945a6
        // ^(http|https):// -> https://
        // ((\w+\.)+) -> www.bilibili.
        // ([a-zA-Z]+) -> com
        // (/([\w&%#=?/]+))*$ -> /video/BV1fh411y7R8/?p=894&spm_id_from=pageDriver&vd_source=b03ab706681712e193fdc7caf9b945a6
        //Pattern pattern = Pattern.compile("^(http|https)://((\\w+\\.)+)([a-zA-Z]+)(/([\\w-&%#=?./]+))*$");
        Pattern pattern = Pattern.compile("^((http|https)://)?([\\w-]+\\.)+[\\w-]+(/[\\w-&%#=?./]*)?$");
        Matcher matcher = pattern.matcher(input);
        return matcher.find();
    }

    public static void main(String[] args) {
        System.out.println(isURL("https://www.bilibili.com/video/BV1fh411y7R8/?p=" +
                "894&spm_id_from=pageDriver&vd_source=b03ab706681712e193fdc7caf9b945a6"));//true
        System.out.println(isURL("https://gitee.com/deng-chongshuang/java-learning" +
                "-code/tree/master/chapter27/src/regexp/"));//true
    }
}
